package com.exmaple.lemlin.ui.fragment;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.exmaple.lemlin.R;
import com.exmaple.lemlin.app.APPIM;
import com.exmaple.lemlin.callback.OnItemClickFriend;
import com.exmaple.lemlin.entity.FriendGood;
import com.exmaple.lemlin.entity.FriendGoodDao;
import com.exmaple.lemlin.ui.abstracts.AbstractBaseFragment;
import com.exmaple.lemlin.ui.activity.FriendActivity;
import com.exmaple.lemlin.ui.adapter.UserAdapter;
import com.exmaple.lemlin.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class FriendAdditionFragment extends AbstractBaseFragment implements FriendActivity.OnFriendAdditionListener, UserAdapter.OnItemClickListener {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private List<FriendGood> friends = new ArrayList<>();
    private UserAdapter userAdapter;

    private OnItemClickFriend mOnItemClickFriend;

    public static FriendAdditionFragment newInstance() {
        return new FriendAdditionFragment();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mOnItemClickFriend = (OnItemClickFriend) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_friend_addition;
    }

    @Override
    protected void initView() {

        friends.addAll(APPIM.getDaoSession().getFriendGoodDao().queryBuilder()
                .where(FriendGoodDao.Properties.Sub.eq("none")).list());
        userAdapter = new UserAdapter(getActivity(), friends);
        userAdapter.setOnItemClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(userAdapter);
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void initLazyLoad() {

    }

    @Override
    public void onItemClick(View view, int position) {
        if (mOnItemClickFriend != null) {
            mOnItemClickFriend.onItemClickAddition(view, position, friends.get(position));
        }
    }

    @Override
    public void onFriendGood(FriendGood friendGood) {
        userAdapter.addData(APPIM.getDaoSession().getFriendGoodDao().queryBuilder()
                .where(FriendGoodDao.Properties.Sub.eq("none")).list());
    }
}
